﻿using UnityEngine;
using System.Collections;

public class MonsterController : BaseController
{
	// Constants

	const float		c_groundAheadCheckDist	= 4.3f;
	const float		c_dropAheadCheckDist	= 3.0f;

	// Members

	/// <summary>
	/// 지면 디텍터 사용 여부
	/// </summary>
	protected bool useGroundDetector
	{
		get;
		set;
	}

	/// <summary>
	/// 바로 눈앞에 땅이 있는지
	/// </summary>
	protected bool groundAhead
	{
		get;
		private set;
	}

	/// <summary>
	/// 발 앞에 낭떠러지가 있는지
	/// </summary>
	protected bool dropAhead
	{
		get;
		private set;
	}

	/// <summary>
	/// 낭떠러지인지 여부와는 상관없이 바로 앞 바닥이 꺼져있을 때
	/// </summary>
	protected bool noGroundBelow
	{
		get;
		private set;
	}

	static int s_raycastMask	= -1;
	protected static int raycastMask
	{
		get
		{
			if (s_raycastMask == -1)			// 최초 1번만 계산
			{
				s_raycastMask	= 1 << LayerMask.NameToLayer("Obstacle");
			}

			return s_raycastMask;
		}
	}

	protected override void OnFixedUpdate()
	{
		base.OnFixedUpdate();

		if (useGroundDetector)
		{
			Vector2 position	= localT.position;
			Vector2 charDistVec	= Vector2.right * character.directionFactor;
			RaycastHit2D ahead	= Physics2D.Raycast(position + Vector2.up, charDistVec, c_groundAheadCheckDist, raycastMask);
			RaycastHit2D drop	= Physics2D.Raycast(position + charDistVec * c_dropAheadCheckDist, -Vector2.up, 10000f, raycastMask);
			RaycastHit2D nogrnd	= Physics2D.Raycast(position + charDistVec * c_dropAheadCheckDist, -Vector2.up, 1f, raycastMask);

			groundAhead			= ahead.collider != null;
			dropAhead			= drop.collider == null;
			noGroundBelow		= nogrnd.collider == null;
		}
	}
}
